package com.luoran.bartalks.action;

import java.util.Date;
import java.util.Map;

import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.luoran.bartalks.core.UserContext;
import com.luoran.bartalks.core.ext.Pageable;
import com.luoran.bartalks.entity.BaseInfo;
import com.luoran.bartalks.entity.biz.CompanyInfo;
import com.luoran.bartalks.entity.biz.UserInfo;
import com.luoran.bartalks.service.ICompanyService;
import com.luoran.bartalks.service.IUserService;
import com.luoran.bartalks.utils.SysUtil;

@Controller
@RequestMapping("/company")
public class CompanyAction implements Pageable {

	@Autowired
	private ICompanyService companyService;

	@Autowired
	private IUserService userService;

	private static final Logger log = LoggerFactory.getLogger(CompanyAction.class);

	// 展示所有公司列表
	@RequestMapping
	public String item() {
		log.info("获取公司列表>>>>>");
		return "/systemManage/company-list";
	}

	// 分页
	@Override
	public PageQuery<? extends BaseInfo> getQueryList(PageQuery<? extends BaseInfo> pageQuery) {
		if (StringUtils.isEmpty(pageQuery.getOrderBy())) {
			pageQuery.setOrderBy("c.create_time DESC");
			@SuppressWarnings("unchecked")
			Map<String, Object> m = (Map<String, Object>) pageQuery.getParas();
			m.put("isSystemUser", SysUtil.isSystemUser());
			m.put("company_id", UserContext.get().getCompanyId());
		}
		return companyService.queryList(pageQuery);
	}

	// 添加公司.同时静默添加一个系统管理员
	@RequestMapping("/save")
	@ResponseBody
	public JSONObject insertCompany(CompanyInfo companyInfo) {
		log.info("添加公司>>>>>>");
		JSONObject obj = new JSONObject();

		if (StringUtils.isEmpty(companyInfo.getCompanyName().trim())) {
			obj.put("msg", "公司名称不能为空");
			obj.put("flag", false);
			return obj;
		}

		// 校验公司名称
		log.info("校验公司名称是否被占用>>>>");
		JSONObject checkName = checkCompanyInfo(companyInfo.getCompanyName().trim());

		if (!checkName.getBooleanValue("flag")) {
			log.info("该公司名称已被占用<<");
			return checkName;
		}

		if (StringUtils.isEmpty(companyInfo.getCompanyCode().trim())) {
			obj.put("msg", "公司编码不能为空");
			obj.put("flag", false);
			return obj;
		}

		// 管理员帐号
		String regex = "^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
		if (!companyInfo.getAdminAccount().matches(regex)) {
			obj.put("msg", "管理员账号需以5-16位字母开头,数字,下划线组成");
			obj.put("flag", false);
			return obj;
		}

		// 管理员帐号唯一性校验
		log.info("校验管理员账号唯一性>>>>");
		JSONObject checkUserName = checkUserName(companyInfo.getAdminAccount());
		if (!checkUserName.getBooleanValue("flag")) {
			log.info("该账号已被占用<<");
			return checkUserName;
		}

		if (StringUtils.isEmpty(companyInfo.getContactPeople().trim())) {
			obj.put("msg", "联系人不能为空");
			obj.put("flag", false);
			return obj;
		}
		if (StringUtils.isEmpty(companyInfo.getContactNumber().trim())) {
			obj.put("msg", "联系电话不能为空");
			obj.put("flag", false);
			return obj;
		}

		companyInfo.setStatus(0);
		companyInfo.setCreateTime(new Date());
		// 添加到数据库.返回的是静默添加的系统管理员的ID
		String uuid = companyService.insertCompany(companyInfo);

		if (uuid != null) {
			log.info("新增公司,管理员>>>>成功");
			obj.put("msg", "操作成功");
			obj.put("flag", true);
			return obj;
		} else {
			log.info("新增公司,管理员>>>>失败");
			obj.put("msg", "操作失败,请重试!");
			obj.put("flag", false);
			return obj;
		}

	}

	// 根据id获取该记录信息
	@RequestMapping("/showItem/{id}")
	@ResponseBody
	public CompanyInfo getCompanyInfo(@PathVariable("id") String id) {
		return companyService.getCompanyInfo(id);

	}

	// 根据id更新
	@RequestMapping("/updateItem")
	@ResponseBody
	public JSONObject updateCompanyInfo(CompanyInfo companyInfo) {

		JSONObject result = new JSONObject();
		if (StringUtils.isEmpty(companyInfo.getContactPeople().trim())) {
			result.put("flag", false);
			result.put("msg", "请填写联系人");
			return result;
		}

		if (StringUtils.isEmpty(companyInfo.getContactNumber().trim())) {
			result.put("flag", false);
			result.put("msg", "请填写联系电话");
		}

		companyInfo.setUpdateTime(new Date());

		int count = companyService.updateCompanyInfo(companyInfo);
		if (count != 0) {
			result.put("msg", "更新成功");
			result.put("flag", true);
			return result;
		}
		result.put("msg", "更新失败,请重试");
		result.put("flag", false);
		return result;

	}

	// 根据id删除
	@RequestMapping("/deleteItem/{id}")
	@ResponseBody
	public JSONObject deleteCompanyInfo(@PathVariable("id") String id) {
		JSONObject json = new JSONObject();
		CompanyInfo info = new CompanyInfo();
		info.setId(id);
		info.setStatus(1);
		int result = companyService.updateCompanyInfo(info);
		if (result != 0) {
			json.put("msg", "删除成功");
			json.put("flag", true);
			return json;
		}
		json.put("msg", "删除失败,请重试");
		json.put("flag", false);
		return json;

	}

	// 公司名称唯一性校验.
	public JSONObject checkCompanyInfo(String companyName) {
		CompanyInfo info = companyService.checkCompanyInfo(companyName);
		JSONObject res = new JSONObject();
		if (info != null) {
			res.put("flag", false);
			res.put("msg", "该公司名称已被占用");
			return res;
		} else {
			res.put("flag", true);
			return res;
		}
	}

	// 管理员账号唯一性校验
	public JSONObject checkUserName(String adminAccount) {
		JSONObject result = new JSONObject();
		UserInfo user = userService.checkUserName(adminAccount);
		if (user != null) {
			result.put("flag", false);
			result.put("msg", "该账号已被占用");
			return result;
		}
		result.put("flag", true);
		return result;
	}

}
